﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Xeml.Document
{
    public class Value : ValueBase
    {
        #region Fields
        private string _value;
        private ValueCollection _collection;
        #endregion

        #region Properties

        /// <summary>
        /// The raw value.
        /// </summary>
        public string Val
        {
            get
            {
                return this._value;
            }
            set
            {
                if (this._value == null || !this._value.Equals(value))
                {
                    this._value = value;
                    OnPropertyChanged("Val");
                }
            }
        }

        public ValueCollection Collection
        {
            get
            {
                return _collection;
            }
            set
            {
                _collection = value;
            }
        }


        #endregion

        #region Constructors
        /// <summary>
        /// The standard constructor.
        /// </summary>
        public Value()
            : base()
        {
        }


        /// <summary>
        /// Parameterized contructor.
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="val"></param>
        public Value(string val)
            : this()
        {
            this._value = val;
        }

        /// <summary>
        /// The copy constructor.
        /// </summary>
        /// <param name="val">The proptotype for the copy process.</param>
        public Value(Value val)
            : base(val)
        {
            this._value = val.Val;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Converts the raw value to double
        /// </summary>
        /// <returns>The double representation.</returns>
        public double DoubleValue
        {
            get
            {
                double ret;
                if (Double.TryParse(this._value, out ret))
                {
                    return ret;
                }
                else
                {
                    return Double.NaN;
                }
            }
            set
            {
                this.Val = value.ToString();
            }
        }

        /// <summary>
        /// Converts the raw value to boolean
        /// </summary>
        /// <remarks>Returns always false if the value field can't be parsed as boolean.</remarks>
        /// <returns>The boolean representation.</returns>
        public bool BooleanValue
        {
            get
            {
                bool ret;
                if (Boolean.TryParse(this._value, out ret))
                {
                    return ret;
                }
                else
                {
                    return false;
                }
            }
            set
            {
                this.Val = value.ToString();

            }
        }


        /// <summary>
        /// Converts this value to it's string representation.
        /// </summary>
        /// <returns>The string representation of this object.</returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}", this._value);
            return sb.ToString();
        }
        #endregion
    }
}
